package com.hibase.core.rule.mapper.ruleexecute;


import com.hibase.core.rule.utils.HiBaseSqlUtils;
import com.hibase.common.exception.HibaseAssert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.SelectProvider;

import java.util.List;
import java.util.Map;

/**
 * 描述:
 *
 * @author li-wei-hua
 * @date 2020-03-19 23:53
 */
public interface RuleExecuteMapper {

    @SelectProvider(type = RuleExecuteSelectProvider.class, method = "select")
    List<Map<String, Object>> select(@Param("sql") String sqlStr, @Param("args") Object... args);


    class RuleExecuteSelectProvider {

        public String select(Map<String, Object> map) {

            // 获取sql
            String sql = (String) map.get("sql");
            // 参数字符串化处理
            Object[] args = (Object[]) map.get("args");
            // 限制为查询语句
            HibaseAssert.isTrue(sql.trim().toLowerCase().startsWith("select"), "请输入正确的查询语句");

            return HiBaseSqlUtils.disposeSql(sql, args);

        }
    }

    @SelectProvider(type = RuleExecuteSelectDebugProvider.class, method = "selectDebug")
    List<Map<String, Object>> selectDebug(@Param("sql") String sqlStr, @Param("args") Object... args);

    class RuleExecuteSelectDebugProvider {

        public String selectDebug(Map<String, Object> map) {

            // 获取sql
            String sql = (String) map.get("sql");
            // 参数字符串化处理
            Object[] args = (Object[]) map.get("args");
            // 限制为查询语句
            HibaseAssert.isTrue(sql.trim().toLowerCase().startsWith("select"), "请输入正确的查询语句");
            //  debug模式需要限制50条数量限制
            return HiBaseSqlUtils.disposeSqlLimit50(sql, args);

        }
    }

    @SelectProvider(type = RuleExecuteSelectByMapProvider.class, method = "selectByMap")
    List<Map<String, Object>> selectByMap(@Param("sql") String sqlStr, @Param("args") Map<String, Object> params);

    class RuleExecuteSelectByMapProvider {

        public String selectByMap(Map<String, Object> map) {

            // 获取sql
            String sql = (String) map.get("sql");
            // 参数字符串化处理
            Map<String, Object> args = (Map<String, Object>) map.get("args");
            // 限制为查询语句
            HibaseAssert.isTrue(sql.trim().toLowerCase().startsWith("select"), "请输入正确的查询语句");

            return HiBaseSqlUtils.disposeSqlByMap(sql, args);

        }
    }

}
